﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

  public partial class WebUserControl0 : deFramework.Base.BaseUserControl
    {

         

        #region "Steps to Implement Customizable Variables"


        // 2. Specify the customizable variables for this User Control within this region. 

        #region "Customizable Variables"

        private int LoopCount = 3;
        private string LoopMessage = "Unspecified Loop Message";
        private string SystemErrorLabel = "Unspecified SYSTEM ERROR";

        #endregion


        /// <summary>
        /// 3. Customize this value for your user control to provide default values for all your customizable variables. 
        /// </summary>
        private string kDefaultUserControlParameters =
            "<V1><LoopCount>5</LoopCount><LoopMessage>Hello World</LoopMessage><SystemErrorLabel>SYSTEM ERROR</SystemErrorLabel></V1>";




        /// <summary>
        /// Make changes only in certain places here 
        /// </summary>
        /// <returns></returns>
        private bool ValidateCustomUserControlConfiguration()
        {
            bool _result = false;
            try
            {
                // 4. Use a version number for your customizable user control variables in order to safely manage backwards compatibility 

                if (this.UserControlParameters.StartsWith("<V1>"))
                {
                    // 5. For each customizable variable make a call to a method for its data type to allow for web part customization.  
                    this.LoopCount = this.GetIntOverrideConfigVariableWithUserControlParamValue(
                        "LoopCount", this.LoopCount);
                    this.LoopMessage = this.GetOverrideConfigVariableWithUserControlParamValue(
                        "LoopMessage", this.LoopMessage);
                    this.SystemErrorLabel = this.GetOverrideConfigVariableWithUserControlParamValue(
                        "SystemErrorLabel", this.SystemErrorLabel);

                    _result = true;
                }
                else if (this.UserControlParameters.Trim().ToLower() == "help")
                    this.PanelContent.InnerHtml = this.UserControlParametersHelpContent();
                else if (this.UserControlParameters.Length > 1)
                    this.PanelContent.InnerHtml = "Web Part Configuration requires User Control Parameters setting to use V1 parameters set. Please contact your Site Owner.";
            }
            catch { }
            return _result;
        }

        /// <summary>
        /// 6. Only customize the lineXX string value assignments and the list of line parameters in the return String.Format 
        /// command of this method to provide user control specific help for customizing parameter values.  
        /// </summary>
        /// <returns></returns>
        private string UserControlParametersHelpContent()
        {
            string lineHead1 = "<H4>CMAP/Alex1Demo.ascx &nbsp; &nbsp; User  Control  Parameters  Help</H4>";
            string lineHead2 = "<table border=1><tr><td><b>Variable</b></td><td><b>Description</b></td><td><b>Example</b></td></tr>";
            string line1 = "<tr><td>LoopCount</td><td>Number of times to loop through message display.</td><td>5</td></tr>";
            string line2 = "<tr><td>RefreshSeconds</td><td>Text that displays in each message display.</td><td>Hello World</td></tr>";
            string line3 = "<tr><td>SystemErrorLabel</td><td>If an error/exception occurs then this setting specifies what the end user will see.</td><td>SYSTEM ERROR</td></tr>";
            string lineFoot = "</table>&nbsp;&nbsp;<b>Example</b>:&nbsp;<input type=text size=120 value=\"" + this.kDefaultUserControlParameters + "\" />";
            string kHelp = "&nbsp;<br />{0}&nbsp;<br />{1}{2}{3}{4}{5}{6}{7}{8}{9}{10}<br />&nbsp;<br />{11}&nbsp;<br />&nbsp;";
            return String.Format(kHelp, lineHead1 + lineHead2,
                line1, line2, line3,
                lineFoot);
        }

        // 7. Do not change the OnInit and OnLoad methods below. Your intial user control specific code should be done in the
        //    ProcessingForOnLoad method that gets called in the OnLoad method if the variables customization process 
        //    validates/completes successfully.  

        // 8. At design-time on your web parts page in your de Control Container web part properties set 
        //    the property values as follows: 
        //      UserControlPath:       ~/_controltemplates/FEATURE-NAME/USER-CONTROL-NAME.ascx
        //      UserControlParameters: help 
        //    Be sure to substitute FEATURE-NAME and USER-CONTROL-NAME with appropriate values for this user control. 

        // 9. Once the help renders and based on your desired customizations for the customizable variables 
        //    make the changes to the UserControlParameters by following the help display.  

        // 10. Do cleanup on any of these instructions that you no longer need once you have setup your user control 
        //     with the CCWP and established/implemented your customizable variables. 

        #endregion

        #region "OnInit, OnLoad, ProcessingForOnLoad"


        /// <summary>
        /// Do not change this method.     
        /// </summary>
        /// <param name="e"></param>
        protected override void OnInit(EventArgs e)
        {
            this.UserControlParameters = this.kDefaultUserControlParameters;
            base.OnInit(e);
        }


        /// <summary>
        /// Do not change this method. 
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            try
            {
                if (this.ValidateCustomUserControlConfiguration())
                {
                    this.ProcessingForOnLoad();
                }
                else
                    this.ShowErrorInPanel();
            }
            catch (Exception ex)
            {
                this.CurrentException = ex;
                this.ShowErrorInPanel();
            }
        }

        /// <summary>
        /// This is where the user control logic for the OnLoad event should go. 
        /// </summary>
        private void ProcessingForOnLoad()
        {
            this.SampleDisplayProcessing(
                this.LoopCount,
                this.LoopMessage,
                this.SystemErrorLabel
                );
        }

        #endregion

        private void SampleDisplayProcessing(int iterationsCount, string iterationMessage, string aErrorMessage)
        {
            try
            {
                if (iterationsCount > 0)
                {
                    for (int i = 0; i < iterationsCount; i++)
                        this.PanelContent.InnerHtml += "<P>&nbsp;Message:&nbsp;" + iterationMessage;
                }
                else
                {
                    this.PanelContent.InnerHtml = "Error Message: " + aErrorMessage;
                }
            }
            catch (Exception ex)
            {
                this.CurrentException = new Exception(aErrorMessage, ex);
            }
        }

         

    
    
    }
 